/**
 * @author - lhn
 * @version - 1.0.0
 * @description - 权限相关 hooks
 * @file - useAuthority.ts
 * @datetime - 2025/2/28
 * @editor - lhn
 */


import type { IAuthority, IAuthorityType } from './IAuthority.ts';

/**
 * 位运算实现权限相关
 * * {object} authorityType - 权限类型
 * * {function} permissionSwitching - 权限的切换
 * * {function} permissionJudgment - 权限的判断
 * */
class Authority implements IAuthority {


  public authorityType: IAuthorityType = {
    READ: 0b1,  // 0001 读
    WRITE: 0b10,  // 0010 写
    UPDATE: 0b100,  // 0100 更新
    DEL: 0b1000  // 1000 删除
  };

  constructor() {
  }

  // 权限组合
  // use-authority.authorityType.READ | use-authority.authorityType.DEL

  /** 权限的切换 */
  public permissionSwitching(author: number, type: number): number {
    return author ^ type;
  }

  /** 权限的判断 */
  public permissionJudgment(author: number, type: number): boolean {
    // author & type ? true : false
    return !!(author & type);
  }


}

const useAuthority: Authority = new Authority();
export default useAuthority;
